package com.ccp.dev.demo.service;

import com.alibaba.fastjson.JSON;
import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.demo.dao.WDemozibDao;
import com.ccp.dev.demo.dao.WDemozbDao;
import com.ccp.dev.demo.model.WDemozb;
import com.ccp.dev.demo.model.WDemozib;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;

/**
 * demo主 信息操作服务类
 *
 * @author cai
 * @date 2020-05-25 10:27:45
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WDemozbService extends BaseService<WDemozb> {

    //主表dao
    @Resource
    private WDemozbDao wDemozbDao;

    //子表dao
    @Resource
    private WDemozibDao wDemozibDao;

    //新增demo
    @Transactional(rollbackFor = Exception.class)
    public int addDemo(String data) {
        JSONObject json = JSONObject.fromObject(data);
        //主表新增
        Object main = json.get("main");
        JSONObject mainJson = JSONObject.fromObject(main.toString());
        Object mainFields = mainJson.get("fields");
        WDemozb wDemozb = JSON.parseObject(mainFields.toString(),WDemozb.class);
        wDemozb.setId(UUIDUtils.getUUIDFor32());
        wDemozb.setTenantId(ContextUtil.getCurrentUserTenantId());
        wDemozb.setCreateBy(ContextUtil.getCurrentUserId());
        wDemozb.setCreateTime(new Date());
        int result = wDemozbDao.add(wDemozb);
        //子表新增
        JSONArray arySub = json.getJSONArray("sub");
        for (int i = 0; i < arySub.size(); i++) {
            final Object subTableObj = arySub.get(i);
            JSONObject subJson = JSONObject.fromObject(subTableObj.toString());
            Object subFields = subJson.get("fields");
            List<WDemozib> wDemozibList = JSON.parseArray(subFields.toString(),WDemozib.class);
            for (WDemozib wDemozib:wDemozibList) {
                wDemozib.setId(UUIDUtils.getUUIDFor32());
                wDemozib.setRefid(wDemozb.getId());
            }
            wDemozibDao.addList(wDemozibList);
        }
        return result;
    }

    //修改demo
    @Transactional(rollbackFor = Exception.class)
    public int updateDemo(String mainTableId, String data) {
        JSONObject json = JSONObject.fromObject(data);
        //主表修改
        Object main = json.get("main");
        JSONObject mainJson = JSONObject.fromObject(main.toString());
        Object mainFields = mainJson.get("fields");
        WDemozb wDemozb = JSON.parseObject(mainFields.toString(),WDemozb.class);
        wDemozb.setId(mainTableId);
        wDemozb.setUpdateBy(ContextUtil.getCurrentUserId());
        wDemozb.setUpdateTime(new Date());
        int result = wDemozbDao.update(wDemozb);
        //删除之前的子表数据
        wDemozibDao.delByRefId(mainTableId);
        //子表新增
        JSONArray arySub = json.getJSONArray("sub");
        for (int i = 0; i < arySub.size(); i++) {
            final Object subTableObj = arySub.get(i);
            JSONObject subJson = JSONObject.fromObject(subTableObj.toString());
            Object subFields = subJson.get("fields");
            List<WDemozib> wDemozibList = JSON.parseArray(subFields.toString(),WDemozib.class);
            for (WDemozib wDemozib:wDemozibList) {
                wDemozib.setId(UUIDUtils.getUUIDFor32());
                wDemozib.setRefid(wDemozb.getId());
            }
            wDemozibDao.addList(wDemozibList);
        }
        return result;
    }

    //获取demo主子表数据
    public Map getMainSubFormByMainId(String mainTableId) {
        WDemozb wDemozb = wDemozbDao.getById(mainTableId);
        List<WDemozib> wDemozibList = wDemozibDao.getWDemozibListByRefId(mainTableId);
        Map map = new HashMap<String,Object>();
        map.put("wDemozb",wDemozb);
        map.put("wDemozibList",wDemozibList);
        return map;
    }

    //获取demo主子表数据
    public Map getMainSubFormByMainId1(String mainTableId) {
        WDemozb wDemozb = wDemozbDao.getById(mainTableId);
        List<WDemozib> wDemozibList = wDemozibDao.getWDemozibListByRefId(mainTableId);
        //封装子表数据
        List subList = new ArrayList<>();
        Map subMap = new HashMap<String,Object>();
        subMap.put("wDemozibList",wDemozibList);
        subMap.put("tableName","demozib");
        subList.add(subMap);

        Map map = new HashMap<String,Object>();
        map.put("wDemozb",wDemozb);
        map.put("subList",subList);
        return map;
    }
}
